﻿#region

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Radovici.Ctrls.ContentControls;
using Radovici.SharedLibrary;
using YouTubePlaylist.Libraries;
using YouTubePlaylist.MusicVideoService;
using YouTubePlaylist.ViewModels;
using MessageBox=Radovici.Ctrls.MessageBox;

#endregion

namespace YouTubePlaylist.Views
{
    public partial class PlaylistOptionsCtrl
    {
        public PlaylistOptionsCtrl()
        {
            InitializeComponent();
        }

        public PlaylistsViewModel ViewModel
        {
            get { return DataContext as PlaylistsViewModel; }
        }

        private void OnEdit(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                VideoPlaylist p = ViewModel.PlaylistsModel.SelectedPlaylist;
                if (p != null)
                {
                    var widget = new Widget
                                     {
                                         Content = new PlaylistInputCtrl(p),
                                         Title = "Edit Playlist",
                                         OkEnabled = true
                                     };
                    widget.Show(OnPlaylistEdited, sender as UIElement);
                }
            }
        }

        private void OnPlaylistEdited(object ctrl, EventArgs e)
        {
            var inputCtrl = ctrl as PlaylistInputCtrl;
            if (inputCtrl != null)
            {
                var playlist = inputCtrl.DataContext as VideoPlaylist;
                if (playlist != null)
                {
                    ViewModel.PlaylistsModel.SavePlaylist(playlist);
                }
            }
        }

        private void OnDeletePlaylist(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                var widget = new Widget
                                 {
                                     Content =
                                         new MessageBox
                                             {
                                                 Message =
                                                     string.Format("Are you sure you want to delete {0}?",
                                                                   ViewModel.PlaylistsModel.SelectedPlaylist.Name)
                                             },
                                     Title = "Delete Playlist",
                                     OkEnabled = true
                                 };
                widget.Show(OnDeletePlaylistConfirmed, sender as UIElement);
            }
        }

        private void OnDeletePlaylistConfirmed(object sender, EventArgs e)
        {
            ViewModel.PlaylistsModel.DeletePlaylist(ViewModel.PlaylistsModel.SelectedPlaylist);
        }

        private void OnCopyPlaylist(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                ViewModel.PlaylistsModel.CopyPlaylist(ViewModel.PlaylistsModel.SelectedPlaylist);
            }
        }

        private void OnCategorizePlaylist(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                VideoPlaylist p = ViewModel.PlaylistsModel.SelectedPlaylist;
                if (p != null)
                {
                    var widget = new Widget
                                     {
                                         Content = new PlaylistCategoryCtrl(p),
                                         Title = "Categorize Playlist",
                                         OkEnabled = true
                                     };
                    widget.Show(OnPlaylistCategorySelected, sender as UIElement);
                }
            }
        }

        private void OnPlaylistCategorySelected(object ctrl, EventArgs e)
        {
            var myCtrl = ctrl as PlaylistCategoryCtrl;
            if (myCtrl != null)
            {
                VideoPlaylist playlist = myCtrl.Playlist;
                if (playlist != null)
                {
                    ViewModel.PlaylistsModel.CategorizePlaylist(playlist, myCtrl.Category.Text);
                }
            }
        }

        private void OnLink(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                VideoPlaylist p = ViewModel.PlaylistsModel.SelectedPlaylist;
                if (p != null)
                {
                    Clipboard.SetText(MailClient.GetLink(p));
                }
            }
        }

        private void OnMail(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                VideoPlaylist p = ViewModel.PlaylistsModel.SelectedPlaylist;
                if (p != null)
                {
                    var widget = new Widget
                                     {
                                         Content = new MailLinkInputCtrl(ViewModel.PlaylistsModel.Model.User, p, null),
                                         Title = "Email a Friend",
                                         OkEnabled = true
                                     };
                    widget.Show(OnMailSent, sender as UIElement);
                }
            }
        }

        private void OnMailSent(object ctrl, EventArgs e)
        {
            var myCtrl = ctrl as MailLinkInputCtrl;
            if (myCtrl != null)
            {
                MailClient.SendMail(myCtrl.From.Text, myCtrl.To.Text, myCtrl.Subject.Text, myCtrl.Body);
            }
        }

        private void OnOptionMouseEnter(object sender, MouseEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                var option = btn.Tag as string;
                if (option != null)
                {
                    ViewModel.PlaylistsModel.SelectedOption = option;
                }
            }
        }

        private void OnOptionMouseLeave(object sender, MouseEventArgs e)
        {
            var btn = sender as Button;
            if (btn != null)
            {
                var option = btn.Tag as string;
                if (option != null)
                {
                    if (ViewModel.PlaylistsModel.SelectedOption == option)
                    {
                        ViewModel.PlaylistsModel.SelectedOption = null;
                    }
                }
            }
        }

        private void OnOptionsMouseMove(object sender, MouseEventArgs e)
        {
            ViewModel.IsActive = true;
        }
    }
}